﻿using Rabbit.Model; 
using System.Collections.Generic;
using System;
using System.Linq;
namespace Rabbit.BusinessLogicLayer
{
    public  class LettersBL:AutoUpdateMessageClassBase<Letter>
    {
        public LettersBL(double UpdateInterval):base(UpdateInterval)
        {

        }
        private List<Letter> GetMyReceivedLetters(double? lastSinceID)
        {
            if (!CurrentPageCursorID.HasValue)
            {
                CurrentPageCursorID = 1;
            }

            string parameter=string.Empty;
            if (lastSinceID == null)
            {
                parameter ="count=" + Utils.Configuration.PageSize + "&page=" + CurrentPageCursorID;
            }
            else
            {
                parameter = "since_id=" + lastSinceID.Value;
            }
          
            string result = RequestHelper.SendOAuthRequest("/direct_messages.json", parameter);
            List<Letter> tmpLetters = WrappedJsonHelper.Deserialize<List<Letter>>(result);

            FetchedMessageCount = tmpLetters.Count;

            return tmpLetters;
        }

         /// <summary>
         /// remember to catch exception when calling this method
         /// </summary>
         /// <returns></returns>
        public List<Letter> GetMySentLetters()
        {
            string result = RequestHelper.SendOAuthRequest("/direct_messages/sent.json", "count=" + Utils.Configuration.PageSize);
            List<Letter> tmpLetters = WrappedJsonHelper.Deserialize<List<Letter>>(result);
            return tmpLetters;
        }

        #region Letter Operation:Send/Delete
        public Letter SendLetter(long userID, string text)
        {
            string result = RequestHelper.SendOAuthRequest("/direct_messages/new.json", "user=" + userID.ToString() + "&text=" + Rabbit.Utils.UrlEncoder.UrlOAuthEncode(text), "Post");
            Letter letter = WrappedJsonHelper.Deserialize<Letter>(result);
            return letter;
        }

        public HashObject DeleteInBoxLetter(long letterID)
        {
            string result = RequestHelper.SendOAuthRequest("direct_messages/destroy/" + letterID.ToString() + ".json?type=in", "", "DELETE");
            HashObject hash = WrappedJsonHelper.Deserialize<HashObject>(result);
            return hash;
        }

        public HashObject DeleteOutBoxLetter(long letterID)
        {
            string result = RequestHelper.SendOAuthRequest("direct_messages/destroy/" + letterID + ".json?type=out", "", "DELETE");
            HashObject hash = WrappedJsonHelper.Deserialize<HashObject>(result);
            return hash;
        }
        #endregion

        public override bool CanGetPreviousePage
        {
            get
            {
                return CurrentPageCursorID > 1;
            }
        }

        protected override void FetchNewMessage()
        {
            List<Letter> newLetterList = new List<Letter>();
            newLetterList = GetMyReceivedLetters(base.LastMaxID);
            if (newLetterList.Count>0)
	        {
                base.LastMaxID = newLetterList.Max(s=>s.Id);
                newLetterList.ForEach(l => l.IsNew = true);
	        }
            RaiseNewMessageReceivedEvent(newLetterList); 
        }

        protected override void FetchMessage()
        {
            List<Letter> tempLetterList = GetMyReceivedLetters(null);
            base.LastMaxID = tempLetterList.Max(s => s.Id);
            RaiseMessageFetchedEvent(tempLetterList);
        }

        public override List<Letter> GetNextPageMessage()
        {
            CurrentPageCursorID++;
            return GetMyReceivedLetters(null);
        }

        public override List<Letter> GetPreviousePageMessage()
        {
            CurrentPageCursorID--;
            return GetMyReceivedLetters(null);
        }

        public override List<Letter> GetFirstPageMessage()
        {
            CurrentPageCursorID = 1;

            List<Letter> letters = GetMyReceivedLetters(null);
            if (letters.Count > 0)
            {
                base.LastMaxID = letters.Max(s => s.Id);
            }
            return letters;
        }
    }
}
